package com.mcxx.modules.minimumliving.controller;

import com.github.pagehelper.util.StringUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.AreaMoveService;
import com.mcxx.modules.miniSalvation.order.dto.AreaMoveDTO;
import com.mcxx.modules.miniSalvation.order.dto.MoveAreaConfirmDTO;
import com.mcxx.modules.miniSalvation.order.dto.MoveAreaDTO;
import com.mcxx.modules.miniSalvation.order.param.MoveAreaParam;
import com.mcxx.modules.miniSalvation.order.service.read.AreaMoveReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Validated
@Api("批量迁移")
@RestService({"minimumliving/migration/v2"})
public class MigrationMgrController extends DefaultRestService {
    private static final Logger logger = LoggerFactory.getLogger(MigrationMgrController.class);
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private AreaMoveService areaMoveService;
    @Autowired
    private AreaMoveReadService areaMoveReadService;


    @PostMapping("getMigrationList")
    @ApiOperation("查询救助家庭列表")
    public void  getMigrationList(MoveAreaParam param, PageParam pageParam ){
        log.info("查询救助家庭列表,参数：{}", param);
        param.setStartDate(DateUtil.getMonthString());
        param.setEndDate(DateUtil.getMonthString());
        Page page = orderReadService.getMigrationList(param, pageParam, getCurUserAccount());
        this.setPageBean(page);
        log.info("查询救助家庭列表,结果：{}", page);
    }

    @PostMapping("exportMigrationList")
    @ApiOperation("导出批量迁移办理列表页")
    public void exportMigrationList(HttpServletResponse response, @RequestBody MoveAreaParam param)throws IOException {
        logger.info("开始导出批量迁移办理列表页，参数是：{}", param);
        UserAccountVo accountVo = getCurUserAccount();
        param.setStartDate(DateUtil.getMonthString());
        param.setEndDate(DateUtil.getMonthString());
        ExcelInfo excelInfo  = areaMoveService.exportMigrationList(param, accountVo);
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出批量迁移办理列表页结束，参数是：{}", param);
    }


    @PostMapping("batchMigration")
    @ApiOperation("批量迁移操作")
    public void batchMigration(@RequestBody @V("notnull") MoveAreaDTO moveAreaDTO){
        logger.info("批量迁移，参数是：{}", moveAreaDTO);
        areaMoveService.batchMigration(moveAreaDTO, getCurUserAccount());
        logger.info("批量迁移，结果是：{}", true);
        setData(true);
    }


    @PostMapping("getBatchMigrationList")
    @ApiOperation("查询批量迁移列表")
    public void getBatchMigrationList(AreaMoveDTO param, PageParam pageParam ){
        log.info("查询批量迁移列表,参数：{}", param);
        param.setMigrationType("1");
        Page page = areaMoveReadService.getBatchMigrationList(param, pageParam, getCurUserAccount());
        this.setPageBean(page);
        log.info("查询批量迁移列表,结果：{}", page);
    }

    @PostMapping("getBatchMigrationGetList")
    @ApiOperation("查询批量迁移列表")
    public void getBatchMigrationGetList(AreaMoveDTO param, PageParam pageParam ){
        log.info("查询批量迁移列表,参数：{}", param);
        param.setMigrationType("2");
        Page page = areaMoveReadService.getBatchMigrationList(param, pageParam, getCurUserAccount());
        this.setPageBean(page);
        log.info("查询批量迁移列表,结果：{}", page);
    }


    @PostMapping("confirmBatchMigration")
    @ApiOperation("确认批量迁移")
    public void confirmBatchMigration(@RequestBody @V("notnull") MoveAreaConfirmDTO moveAreaConfirmDTO){
        log.info("查询批量迁移列表,参数：{}", moveAreaConfirmDTO);
        areaMoveService.confirmMigration(moveAreaConfirmDTO, getCurUserAccount());
        log.info("查询批量迁移列表,结果：{}", true);
        setData(true);
    }


    @PostMapping("rejectMigration")
    @ApiOperation("驳回批量迁移")
    public void rejectMigration(@RequestBody @V("notnull")MoveAreaConfirmDTO moveAreaConfirmDTO){
        log.info("查询批量迁移列表,参数：{}", moveAreaConfirmDTO);
        areaMoveService.rejectMigration(moveAreaConfirmDTO, getCurUserAccount());
        log.info("查询批量迁移列表,：结果是{}", true);
        setData(true);
    }


    @PostMapping("auditBatchMigration")
    @ApiOperation("审批批量迁移")
    public void auditBatchMigration(MoveAreaParam param){
        UserAccountVo user = this.getCurUserAccount();
        param.setUserId(user.getUserId());
        param.setOrgId(user.getOrgId());
        param.setAreaCode(user.getAreaCode());
        param.setUserName(user.getUsername());
        BaseResult result = areaMoveService.auditBatchMigration(param);
        if(null != result && StringUtil.isNotEmpty(result.getMsg())){
            throw new BizException("-1",result.getMsg());
        }else{
            setSuccess();
        }
    }



    @PostMapping("downLoadMigMemberList")
    @ApiOperation(value="导出迁移信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出迁移信息")
    public void downLoadMigMemberList(HttpServletResponse response, @RequestBody AreaMoveDTO param)throws IOException {
        log.info("开始查询导出迁移信息，参数是：{}", param);
       // param.setBusinessType(Constant.BusinessCode.DIBAO);
        ExcelInfo excelInfo = areaMoveService.exportExcelMigMember(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        log.info("开始导出迁移信息，查询结果是：{}", excelInfo);

    }

}



